﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.IO;
using System.Runtime.InteropServices;

namespace MeizuSDK.Core
{
    /// <summary>
    /// 设备环境类
    /// </summary>
    public static class DeviceEnv
    {
        #region DllImport
        [DllImport("PlatformAPIWrapper.dll")]
        private static extern int CallNotifyApiGetImeiSerialNumber2(IntPtr hWnd, [OutAttribute()] [MarshalAsAttribute(UnmanagedType.LPWStr)] StringBuilder lpszImeiSerialNumber, int dwLength, [OutAttribute()] [MarshalAsAttribute(UnmanagedType.LPWStr)] StringBuilder lpszDeviceSerialNumber, int dwLength2);

        [DllImport("PlatformAPIWrapper.dll")]
        private static extern uint SettingApiGetHardwareVersionNumber();
        #endregion

        /// <summary>
        /// 获取IMEI和设备序列号
        /// </summary>
        /// <param name="imei">返回IMEI</param>
        /// <param name="serial">设备序列号</param>
        private static void GetSerialNumbers(out string imei, out string serial)
        {
            StringBuilder imeiSB = new StringBuilder(128);
            StringBuilder serialSB = new StringBuilder(128);

            CallNotifyApiGetImeiSerialNumber2(IntPtr.Zero, imeiSB, 256, serialSB, 256);

            imei = imeiSB.ToString();
            serial = serialSB.ToString();
        }

        /// <summary>
        /// 获取手机的IMEI号码
        /// </summary>
        public static string IMEISerialNumber
        {
            get
            {
                string imei, serial;
                GetSerialNumbers(out imei, out serial);
                return imei;
            }
        }
        /// <summary>
        /// 获取手机设备序列号
        /// </summary>
        public static string DeviceSerialNumber
        {
            get
            {
                string imei, serial;
                GetSerialNumbers(out imei, out serial);
                return serial;
            }
        }
        /// <summary>
        /// 获取手机硬件版本号
        /// </summary>
        public static uint HardwareVersion
        {
            get{
                return SettingApiGetHardwareVersionNumber();
            }
        }
    }
}
